package com.vdg.fps.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.vdg.R;
import com.vdg.fps.data.FaceBookAccount;
import com.vdg.fps.data.GroupsData;
import com.vdg.fps.data.Member;
import com.vdg.fps.ui.view.TextViewPlus;
import com.vdg.fps.ulti.Utilities;

public class ListFriendsAdapter extends ArrayAdapter<Member> {
	private List<Member> mListMembers;
	private ArrayList<Member> mOrgListMembers;

	public ListFriendsAdapter(Context context, int textViewResourceId,
			List<Member> objects) {
		super(context, textViewResourceId, objects);
		mOrgListMembers = new ArrayList<Member>();
		mListMembers = objects;
		mOrgListMembers.addAll(mListMembers);
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Member member = mListMembers.get(position);
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(getContext()).inflate(
					R.layout.friends_row, parent, false);
			holder.mTxvName = (TextViewPlus) convertView
					.findViewById(R.id.txv_name);
			holder.mTxvID = (TextViewPlus) convertView
					.findViewById(R.id.txv_id);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (member != null) {
			String name = member.getName();
			String id = member.getID();
			holder.mTxvName.setText(name);
			holder.mTxvID.setText(id);
		}
		return convertView;
	}

	public static class ViewHolder {
		private TextViewPlus mTxvName;
		private TextViewPlus mTxvID;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListMembers.size();
	}

	public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		mListMembers.clear();
		if (charText.length() == 0) {
			mListMembers.addAll(mOrgListMembers);
		} else {
			for (Member wp : mOrgListMembers) {
				if (wp.getName().toLowerCase(Locale.getDefault())
						.contains(charText)) {
					mListMembers.add(wp);
				}
			}
		}
		notifyDataSetChanged();
	}

	public Member getItem(int position) {
		if (mListMembers == null || mListMembers.size() == 0) {
			return null;
		}
		return mListMembers.get(position);
	}

	public List<Member> getDisplayData() {
		return mListMembers;
	}
}
